Partial Public Class pages_Files_usercontrols_UCFiles : Inherits UserControl

#Region "Events"
    Public Event ExportClick()
    Public Event ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer)
#End Region

#Region "Interface"
    Public Sub Display(ByVal files As CFileList, showProduct As Boolean) 'Complete list, not yet paged
        colNumber.Visible = files.Count > 0
        If colNumber.Visible Then col1.ColSpan += 1
        If Not showProduct Then colLast.ColSpan = 2
        colProduct.Visible = showProduct
        rowAdd.Visible = Not showProduct
        CTextbox.OnReturnPress(txtFile, btnAdd)

        'Display
        plh.Controls.Clear() 'Only needed for ajax postbacks
        Dim sorted As IList = Nothing 'Fixes the numbering to reflect a user-sorted list (querystring sortBy)
        Dim page As IList = ctrlPaging.Display(files, sorted) 'In-Memory paging, also outputs the sorted list
        For Each i As CFile In page
            UCFile(plh).Display(i, sorted, showProduct)
        Next
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCFile(ByVal target As Control) As pages_files_usercontrols_UCFile
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.UCFile)
        target.Controls.Add(ctrl)
        Return CType(ctrl, pages_files_usercontrols_UCFile)
    End Function
#End Region

#Region "Event Handlers"
    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExport.Click
        RaiseEvent ExportClick()
    End Sub
    Protected Sub btnResort_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnSortByFileName.Click, btnSortByProduct.Click, btnSortByFileSize.Click
        'Toggle descending if necessary
        Dim sortBy As String = CType(sender, LinkButton).CommandArgument
        Dim descending As Boolean = ctrlPaging.IsDescending
        Dim currentSort As String = ctrlPaging.SortColumn
        If Not String.IsNullOrEmpty(currentSort) Then
            If currentSort = sortBy Then descending = Not descending
        End If

        'Bubble up as event, search page will add filter info and redirect
        RaiseEvent ResortClick(sortBy, descending, ctrlPaging.Info.PageIndex + 1)
    End Sub
#End Region

#Region "Paging"
    Public ReadOnly Property Info() As CPagingInfo
        Get
            Return ctrlPaging.Info
        End Get
    End Property
    Public Property QueryString() As String
        Get
            Return ctrlPaging.QueryString
        End Get
        Set(ByVal value As String)
            ctrlPaging.QueryString = value
        End Set
    End Property
    Public Property PageSize() As Integer
        Get
            Return ctrlPaging.PageSize
        End Get
        Set(ByVal value As Integer)
            ctrlPaging.PageSize = value
        End Set
    End Property
#End Region

    Protected Sub btnAdd_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnAdd.Click
        With New CFile
            .FileProductId = CWeb.RequestInt("productId")
            .FileName = txtFile.Text

            If .FileName.ToLower.Trim.StartsWith("http") Then
                .FileName = Server.UrlDecode(.FileName.Trim)
                .FileName = .FileName.Substring(CConfig.DropboxUrl().Length)
            End If
            .FileSize = 0
            If .FileSize = Long.MinValue Then .FileSize = 0
            .Save()
        End With
        Response.Redirect(Request.RawUrl)
    End Sub
End Class
